﻿<%@ Control Language="C#" ClassName="BundledControl" %>

<script runat="server">
  protected void Page_Load(object sender, EventArgs e)
  {
    if (Page.IsPostBack == false)
    {
      B2C_MALL_ORDER ws = new B2C_MALL_ORDER();
      ws.Url = ConfigurationManager.AppSettings["B2C_MALL_ORDER"].ToString(); 
      string jsonString = ws.B2C_MALL_GetAIRSHOP_Products_Sale(CommonUtil.GetWebDevice()
        , HttpContext.Current.User.Identity.Name);
      JObject restoredObject = JsonConvert.DeserializeObject<JObject>(jsonString);
      string Is_Valid = restoredObject["Is_Valid"].ToString();
      string msg = restoredObject["Msg"].ToString();
      if (Is_Valid == "")
      {
        dynamic dynObj = restoredObject["pProducts_Sale_Ary_List"];
        ListViewAIRSHOP.DataSource = dynObj;
        ListViewAIRSHOP.DataBind();
      }
    }
  }

  protected void LBSaleAddCart_Click(object sender, EventArgs e)
  {
    ListViewDataItem dataItem = (sender as LinkButton).Parent as ListViewDataItem;
    TextBox txtQty = dataItem.FindControl("txtboxQty") as TextBox;
    Literal Lit_B2C_CODE = dataItem.FindControl("Lit_B2C_CODE") as Literal;
    Literal Lit_Output_Prod_Cat_ID = dataItem.FindControl("Lit_Output_Prod_Cat_ID") as Literal;
    Literal Lit_SMAS_CODE = dataItem.FindControl("Lit_SMAS_CODE") as Literal;

    Server.Execute("~/Shopping/AddToCart.aspx?B2C_CODE=" + Lit_B2C_CODE.Text
        + "&SMAS_CODE=" + Lit_SMAS_CODE.Text
        + "&qty=" + txtQty.Text
        + "&catid=" + Lit_Output_Prod_Cat_ID.Text);
    //導向目前此頁，以便更新結帳清單資料
    Response.Redirect(Page.AppRelativeVirtualPath);
  }

  B2C_MALL_HOME homeWS = new B2C_MALL_HOME();  
  protected void ListViewAIRSHOP_ItemDataBound(object sender, ListViewItemEventArgs e)
  {
    if (e.Item.ItemType == ListViewItemType.DataItem)
    {
      Literal Lit_SMAS_CODE = e.Item.FindControl("Lit_SMAS_CODE") as Literal;
      homeWS.Url = ConfigurationManager.AppSettings["B2C_MALL_HOME"].ToString(); 
      string jsonString = homeWS.B2C_MALL_GetQuantity(CommonUtil.GetWebDevice()
        , HttpContext.Current.User.Identity.Name
        , Lit_SMAS_CODE.Text);
      JObject restoredObject = JsonConvert.DeserializeObject<JObject>(jsonString);
      if (restoredObject != null)
      {
        string Is_Valid = restoredObject["Is_Valid"].ToString();
        string msg = restoredObject["Msg"].ToString();
        if (Is_Valid == "Y")
        {
          string CURR_QTY = restoredObject["CURR_QTY"].ToString();
          if (CURR_QTY == "0" || string.IsNullOrEmpty(CURR_QTY))
          {
            LinkButton LBSaleAddCart = e.Item.FindControl("LBSaleAddCart") as LinkButton;
            LBSaleAddCart.OnClientClick = "return false;";            
            LBSaleAddCart.Controls.Clear();
            Image img = new Image();
            img.ImageUrl = "~/Images/Icons/Car.png";
            LBSaleAddCart.Controls.Add(img);
            LBSaleAddCart.Controls.Add(new LiteralControl("商品補貨中!"));
            LBSaleAddCart.ForeColor = System.Drawing.Color.Red;
          }
        }
      }
    }
  }
</script>

<div class="plus-products green with-slide JS_listSlide">
  <h3>好康報報一起買</h3>
  <div class="products-item-container">
    <div class="slide-container">
      <div class="slide-area">
        <asp:ListView ID="ListViewAIRSHOP" runat="server" OnItemDataBound="ListViewAIRSHOP_ItemDataBound">
          <ItemTemplate>
            <div class="products-item">
              <div class="image-container">
                <a href="javascript:void(window.open('../Products/ProductDetailPopup.aspx?B2C_CODE=<%# Server.HtmlEncode(Eval("B2C_CODE").ToString()) %>','','width=760px;Height=490px;menubar=no,location=no,resizable=no,toolbar=no,scrollbars=yes,status=no'));">
                  <img src='<%# ((dynamic)Eval("pIMAGE_PATH_M_Ary_List"))[0].IMAGE_PATH_M %>'>
                </a>
                &nbsp;&nbsp;
              </div>
              <div class="general-info">
                <div class="top-table">
                  <p>
                    <%# Server.HtmlEncode(Eval("B2C_CODE_NAME").ToString()) %>
                  </p>
                  <table>
                    <tr>
                      <th>特價</th>
                      <td><strong>$  <%# int.Parse(Server.HtmlEncode(Eval("ACT_PRICE").ToString())).ToString("n0") %></strong></td>
                    </tr>
                    <tr>
                      <th>數量</th>
                      <td>
                        <asp:TextBox ID="txtboxQty" runat="server" Text="1" ValidationGroup="SaleValid" MaxLength="3" ></asp:TextBox>
                        <asp:RequiredFieldValidator ID="RVQty1" runat="server" ValidationGroup="SaleValid" ErrorMessage="*" ControlToValidate="txtboxQty" ForeColor="Red" Display="Dynamic" SetFocusOnError="true" />
                        <asp:RequiredFieldValidator ID="RVQty2" runat="server" ValidationGroup="SaleValid" ErrorMessage="*" ControlToValidate="txtboxQty" ForeColor="Red" Display="Dynamic" InitialValue="0" SetFocusOnError="true" />
                      </td>
                    </tr>
                  </table>
                </div>
                <asp:LinkButton ID="LBSaleAddCart" runat="server" CssClass="blue-btn"
                  ValidationGroup="SaleValid" OnClick="LBSaleAddCart_Click">
                  <img id="Img1" src="~/Images/Icons/Car.png" runat="server" />
                  加入購物車
                </asp:LinkButton>
                <%--<asp:Label ID="lbl_StockMessage" CssClass="red-text" runat="server" Text="商品補貨中!" Visible="false"/>--%>
              </div>
            </div>
            <asp:Literal ID="Lit_B2C_CODE" runat="server" Text='<%# Server.HtmlEncode(Eval("B2C_CODE").ToString()) %>' Visible="false" />
            <asp:Literal ID="Lit_Output_Prod_Cat_ID" runat="server" Text='<%# Server.HtmlEncode(Eval("Output_Prod_Cat_ID").ToString()) %>' Visible="false" />
            <asp:Literal ID="Lit_SMAS_CODE" runat="server" Text='<%# ((dynamic)Eval("pSMAS_CODE_Ary_List"))[0].SMAS_CODE %>' Visible="false" />
          </ItemTemplate>
        </asp:ListView>
      </div>
    </div>
  </div>
</div>
